<!-- // created by: alfin akhret 
 // created by: alfin akhret
// related files :
// model/mdue_date.php
// view/f-due-date.php
// view/grid-due-date.php
// library/serversidedatatables.php 
-->
<!-- delete confirmation dialog box -->
<div id="delConfDialog" title="Confirm">
    <p>Anda yakin?</p>
</div>
<!-- message dialog box -->
<div id="msgDialog"><p></p></div>

<!-- edited by : alfin akhret
SSP datattables -->
<table id="records" class="handsetRecord">
    <thead>
    </thead>
    <tbody>
    </tbody>
</table>
<!--  -------------->

<script type="text/javascript">
var readUrl   = 'index.php/due_date/get_due_date';
updateUrl = '<?=base_url();?>index.php/due_date/close'
// delUrl    = '<?=base_url();?>index.php/group_alias/delete';
  

$( function() {
    readUsers();
    $( '#msgDialog' ).dialog({
        autoOpen: false,
        buttons: {
            'Ok': function() {
                $( this ).dialog( 'close' );
            }
        }
    });
    
    $( '#delConfDialog' ).dialog({
        autoOpen: false,
        buttons: {
            'No': function() {
                $( this ).dialog( 'close' );
            },
            'Yes': function() {
                //display ajax loader animation here...
                $( '#ajaxLoadAni' ).fadeIn( 'slow' );
                $( this ).dialog( 'close' );
                $.ajax({
                    url: delHref,
                    success: function( response ) {
                        //hide ajax loader animation here...
                        $( '#ajaxLoadAni' ).fadeOut( 'slow' );
                        
                        $( '#msgDialog > p' ).html( response );
                        $( '#msgDialog' ).dialog( 'option', 'title', 'Success' ).dialog( 'open' );
                        
                        $( 'a[href=' + delHref + ']' ).parents( 'tr' )
                        .fadeOut( 'slow', function() {
                            $( this ).remove();
                        });
                        
                    } //end success
                });
            } //end Yes
        } //end buttons
    }); //end dialog
    
    
    $( '#records' ).delegate( 'a.deleteBtn', 'click', function() {
        delHref = $( this ).attr( 'href' );
        $( '#delConfDialog' ).dialog( 'open' );
            return false;
    }); //end delete delegate
    // --- Create Record with Validation ---
}); //end document ready

function readUsers() {
    $('.handsetRecord').dataTable({
                "bJQueryUI": true, // aktifkan tampilan tabel JQueryUI
                "sAjaxSource": readUrl, //data source
                "sAjaxDataProp": "aaData", //variabel yg menampung data table
                "bServerSide": true, //serverside processing true
                "bProcessing": true, // tampilkan 'processing...' status saat data dipanggil
                // "sPaginationType": 'full_numbers',
                "bPaginate": true,
                "aoColumns":[ //tentukan kolom pd tabel yg akan menampung value dari server
                    {"mDataProp": "no_sell_in", "sTitle": "No Sell In"}, 
                    {"mDataProp": "user_name", "sTitle": "Sales"}, 
                    {"mDataProp": "to_char", "sTitle": "Tanggal Sell In"}, 
                    {"mDataProp": "top_day", "sTitle": "Termin"}, 
                    {"mDataProp": "tgl_jatuh_tempo", "sTitle": "Tanggal Jatuh Tempo"}, 
                    {"mDataProp": "jatuh_tempo", "sTitle": "Jatuh Tempo"}
                ],
                "fnRowCallback": function(nRow, aaData, iDisplayIndex, iDisplayIndexFull) 
                {
                    $(nRow).find('td').eq(-1).after('<td><a href="'+updateUrl+'/'+aaData[0]+'">Close</a></td>');

                    //tambahkan detail
                    $(nRow).find('td').eq(3).append(' hari');
                    $(nRow).find('td').eq(5).append(' hari lagi');

                    //$(nRow).find('td').eq(0).before('<td>'+ no++ +'</td>');

                    $.ajax({
                              url: "<?=base_url();?>index.php/due_date/defoger",
                              type: "POST",
                              data: {'str' : aaData[0]},
                              success: function(response) {
                                  $(nRow).find('td').eq(0).replaceWith('<a href="<?=base_url();?>index.php/sell/getDetail/'+aaData[0]+'">'+response+'</a>');
                              }
                       });

                    // $(nRow).find('td').eq(0).replaceWith(function() {
                    //     var url = $.trim($(this).text());
                    //     return '<a href="<?=base_url();?>index.php/sell/getDetail/' + url.replace(/^0+/, '') + '">' + url + '</a>';
                    // });

                    return nRow;
                }
        });
        no=1;
        $('.handsetRecord thead tr').find('th').eq(-1).after('<th class="ui-state-default" width="200px">Action</th>');
        // $('.handsetRecord thead tr').find('th').eq(0).before('<th class="ui-state-default" width="50px">No</th>');
}
</script>
